const express = require('express');
const { sqlFn } = require('../mysql');
const router = express.Router();
const remove = require('lodash/remove');

router.post('/add', (req, res, next) => {
    const sql = "INSERT INTO menu values (null,?,?,?,?,?,?)";
    const { pid = -1, name, linkUrl, openType, icon, isOfAdmin } = req.body;
    const arr = [pid, name, linkUrl, openType, icon, isOfAdmin];
    sqlFn(sql, arr, (err, results) => {
        if (err) {
            return next(err);
        }
        if (results && results.affectedRows) {
            res.send({ code: 200, data: results, success: true });
        } else {
            res.send({ code: 500, message: '插入失败', success: false });
        }
    });
});

router.get('/list', (req, res, next) => {
    const sql = "SELECT * FROM menu";
    sqlFn(sql, [], (err, results) => {
        if (err) {
            return next(err);
        }

        // 递归获取菜单树
        // 推荐的树形结构构建方法
        function buildTree(list, pid = -1) {
            return list
                .filter(item => Number(item.pid) === Number(pid))
                .map(item => {
                    const children = buildTree(list, item.id);
                    return children.length > 0
                        ? { ...item, children }
                        : { ...item };
                });
        }
        // 无论有无数据都返回数组和 code: 200
        res.send({ code: 200, data: buildTree(results), success: true });
    });
});

// 根据pid找到id为pid的节点
router.get('/getMenuName', (req, res, next) => {
    const sql = "SELECT name FROM menu WHERE id = ?";
    const { id } = req.query;
    sqlFn(sql, [id], (err, results) => {
        if (err) {
            return next(err);
        }
        if (results && results.length) {
            res.send({ code: 200, data: results[0].name, success: true });
        } else {
            res.send({ code: 500, message: '查询失败', success: false });
        }
    });
})

router.post('/update', (req, res, next) => {
    const sql = "UPDATE menu SET `name`=?,`linkUrl`=?,`openType`=?,`icon`=?,`isOfAdmin`=? WHERE id = ?";
    const { id, name, linkUrl, openType, icon, isOfAdmin } = req.body;
    sqlFn(sql, [name, linkUrl, openType, icon, isOfAdmin, id], (err, results) => {
        if (err) {
            return next(err);
        }
        if (results && results.affectedRows) {
            res.send({ code: 200, data: results, success: true });
        } else {
            res.send({ code: 500, message: '更新失败', success: false });
        }
    });
})

router.get('/delete', (req, res, next) => {
    // 查询有没有子菜单
    const sql = 'select * from menu where `pid` = ?'
    const { id } = req.query;
    sqlFn(sql, [id], (err, results) => {
        if (err) {
            return next(err);
        }
        if (results && results.length) {
            console.log(results);

            res.send({ code: 500, message: '该菜单下有子菜单，不能删除', success: false });
        } else {
            // 删除菜单
            const sql = 'delete from menu where id = ?'
            sqlFn(sql, [id], (err, results) => {
                if (err) {
                    return next(err);
                }
                if (results && results.affectedRows) {
                    console.log(results);
                    res.send({ code: 200, data: results, success: true, message: '删除成功' });
                } else {
                    console.log(results);
                    res.send({ code: 500, data: results, message: '删除失败', success: false });
                }
            })
        }
    })
})

module.exports = router;